import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: WJW
 * Date: 2024-08-05
 * Time: 22:01
 */
public class ThreadDemo1 {
    public static void main(String[] args) {
        Object A = new Object();
        Object B = new Object();
        Thread t1 = new Thread(()->{
            synchronized (A){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //尝试获取B
                synchronized (B){
                    System.out.println("t1线程拿到了两把锁");
                }
            }
        });
        Thread t2 = new Thread(()->{
            synchronized (B){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //尝试获取A
                synchronized (A){
                    System.out.println("t2线程拿到了两把锁");
                }
            }
        });
        t1.start();
        t2.start();
    }
}
